<?php
/**
 * 'db layer' ;-)
 * 
 * SQLite only ! 
 */
class dbSql
{
  private $dbh;
  private $error;
/*
  public static function noDiacritics($utf8_string)
  { 
   $ascii_string= strtr($utf8_string,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ",
                            "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn"); 
   return $ascii_string; 
  }
*/
  
  /**
   * Registers new UDF
   * 
   * @param string $fname UDF name
   * @param string $callback Real function name         
   */   
  public function registerFn($fname,$callback)
  {
    $this->dbh->createFunction($fname,$callback);
  }
  
  public function __construct($db = DB_PATH)
  {
    $this->error = '';
    $this->dbh = new SQLiteDatabase($db);
  }
  
  public function exec($sql)
  {
    $this->error = '';
    $this->dbh->queryExec($sql,$this->error);
    if(!empty($this->error))
    {
      throw new Exception(STR_ERR_DB.' : '.$this->error);
    }
    else
    {
      return($this->dbh->changes());
    }
  }
  
  public function query($sql)
  {
    $this->error = '';
    $res = $this->dbh->arrayQuery($sql,SQLITE_ASSOC,$this->error);
    if(!empty($this->error))
    {
      throw new Exception(STR_ERR_DB.' : '.$this->error);
    }
    else
    {
      return $res;
    }
  }
  
  public function __destruct()
  {
    $this->dbh = NULL;
  }
}
?>
